<?php
/**
 * Created by PhpStorm.
 * User: lixy
 * Date: 2016/3/8
 * Time: 15:08
 * 生成分销系统分享二维码图片
 */

namespace app\lib;

use app\lib\Tools;
use app\lib\ImageTools;

class Fx_Image
{
//获取字体长宽  ttf
    static function get_bbox($text, $F_SIZE, $F_FONT)
    {
        return imagettfbbox($F_SIZE, 0, $F_FONT, $text);
    }

    static function text_height($text, $F_SIZE, $F_FONT)
    {
        $box = static::get_bbox($text, $F_SIZE, $F_FONT);
        $height = $box[3] - $box[5];
        return $height;
    }

    static function text_width($text, $F_SIZE, $F_FONT)
    {
        $box = static::get_bbox($text, $F_SIZE, $F_FONT);
        $width = $box[4] - $box[6];
        return $width;
    }

    //生成圆形图方画

    static function _get_lt_rounder_corner($radius, $r, $p, $g)
    {


        $img = imagecreatetruecolor($radius, $radius);

        $bgcolor = imagecolorallocate($img, $r, $p, $g);

        $fgcolor = imagecolorallocate($img, 0, 0, 0);

        imagefill($img, 0, 0, $bgcolor);

        imagefilledarc($img, $radius, $radius, $radius * 2, $radius * 2,
            180, 270, $fgcolor, IMG_ARC_PIE);

        imagecolortransparent($img, $fgcolor);

        return $img;

    }

    function _round_it($src_image)
    {
        $image_width = imagesx($src_image);
        $image_height = imagesy($src_image);

        if ($image_width < $image_height) {
            $imageSize = $image_width;
        } else {
            $imageSize = $image_height;
        }

        //$imageSize = 800;//$image_width;
        $radius = (int)$imageSize / 2;

        // create a new image, with src_width, src_height, and fill it with transparent color

        $image = imagecreatetruecolor($imageSize, $imageSize);

        $trans_color = imagecolorallocate($image, 255, 255, 255);

        imagefill($image, 0, 0, $trans_color);


        // then overwirte the source image to the new created image

        imagecopymerge($image, $src_image, 0, 0, 0, 0, $imageSize, $imageSize, 100);


        // then just copy all the rounded corner images to the 4 corners


        // lt

//        $lt_corner = static::_get_lt_rounder_corner($radius, 52, 57, 79);
        $lt_corner = static::_get_lt_rounder_corner($radius, 252,26,99);//头像背景色

        imagecopymerge($image, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100);

        // lb

        $lb_corner = imagerotate($lt_corner, 90, $trans_color);

        imagecopymerge($image, $lb_corner, 0, $imageSize - $radius, 0, 0,
            $radius, $radius, 100);

        // rb

        $rb_corner = imagerotate($lt_corner, 180, $trans_color);

        imagecopymerge($image, $rb_corner, $imageSize - $radius, $imageSize
            - $radius, 0, 0, $radius, $radius, 100);

        // rt

        $rt_corner = imagerotate($lt_corner, 270, $trans_color);

        imagecopymerge($image, $rt_corner, $imageSize - $radius, 0, 0, 0, $radius, $radius, 100);


        // set the transparency

        imagecolortransparent($image, $trans_color);


        return $image;
    }


    static function mergerImg($op)
    {
        $bg_img = imagecreatefromjpeg($op['bg']);

        $header_width=100;
        $header_height=$header_width;
        // 头像
//        $header_img = imagecreatefromjpeg($op['header']);
        $header_data=Tools::http_get_data($op['header']);
        $header_img = imagecreatefromstring ($header_data);
        list($header_w,$header_h)=ImageTools::getImageSize($header_data);
//        list($header_w,$header_h) = getimagesize($op['header']);
//        list($header_w,$header_h)=[640,640];
        $header_t = imagecreatetruecolor($header_width,$header_height);
        $white = imagecolorallocate($header_t, 255, 255, 255);
        imagefill($header_t, 0, 0, $white);
        imagecopyresampled($header_t, $header_img, 0, 0, 0, 0, $header_width, $header_height, $header_w, $header_h);
        imagedestroy($header_img);
        $header_t=static::_round_it($header_t);

        imagecopy($bg_img,$header_t,269,26,0,0,$header_width,$header_height);//头像位置
        imagedestroy($header_t);
        //二维码
        $qr_width=260;
        $qr_height=$qr_width;

//        $qr_img = imagecreatefromjpeg($op['qr']);
        $qr_data=Tools::http_get_data($op['qr']);
        $qr_img = imagecreatefromstring ($qr_data);
        list($qr_w,$qr_h)=ImageTools::getImageSize($qr_data);
//        list($qr_w,$qr_h) = getimagesize($op['qr']);
//        list($qr_w,$qr_h) = [430,430];

        $qr_t = imagecreatetruecolor($qr_width,$qr_height);
        $white = imagecolorallocate($qr_t, 255, 255, 255);
        imagefill($qr_t, 0, 0, $white);
        imagecopyresampled($qr_t, $qr_img, 0, 0, 0, 0, $qr_width, $qr_height, $qr_w, $qr_h);
        imagedestroy($qr_img);




        imagecopy($bg_img,$qr_t,189,517,0,0,$qr_width,$qr_height);//二维码位置
        imagedestroy($qr_t);
        // 文字
        $textcolor = imagecolorallocate($bg_img, 255, 255, 255); //设置一个颜色变量为黑色
        $font=$op['font'];
        $text = $op['text']; //在图像中输出的字符
        $zi_chang=static::text_width($text,17,$font) / 2;

        imagettftext($bg_img,17,0,315-$zi_chang / 2,160,$textcolor,$font,$text);




        return $bg_img;
    }

}

/*
 $imgs = array(
    'header' => 'zfj.jpg',
    'qr' => 'qr.jpg',
    'bg' => './fx_share_bg.jpg',
    'text' => './fx_share_bg.jpg',
    'font' => './msyh.ttc',
);
   ob_clean();
        header("Content-type: image/jpeg");

imagejpeg(mergerImg($imgs));
*/